﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Himp.Platform.Migrations
{
    /// <inheritdoc />
    public partial class add_table_CodgHistory : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "MainCodgDictRule");

            migrationBuilder.DropColumn(
                name: "DefaultFlag",
                table: "MainCodgDict");

            migrationBuilder.RenameColumn(
                name: "Name",
                table: "MainCodgDict",
                newName: "CodgDictName");

            migrationBuilder.RenameColumn(
                name: "Code",
                table: "MainCodgDict",
                newName: "CatgeCodg");

            migrationBuilder.CreateTable(
                name: "MainCodgHistory",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Codg = table.Column<string>(type: "text", nullable: false),
                    CodgDictId = table.Column<Guid>(type: "uuid", nullable: false),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainCodgHistory", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainCodgHistory_MainCodgDict_CodgDictId",
                        column: x => x.CodgDictId,
                        principalTable: "MainCodgDict",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "MainCodgRule",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    CodgDictRuleType = table.Column<int>(type: "integer", nullable: false),
                    Format = table.Column<string>(type: "text", nullable: true),
                    DefaultValue = table.Column<string>(type: "text", nullable: true),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    CodgDictId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainCodgRule", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainCodgRule_MainCodgDict_CodgDictId",
                        column: x => x.CodgDictId,
                        principalTable: "MainCodgDict",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_MainCodgHistory_CodgDictId",
                table: "MainCodgHistory",
                column: "CodgDictId");

            migrationBuilder.CreateIndex(
                name: "IX_MainCodgRule_CodgDictId",
                table: "MainCodgRule",
                column: "CodgDictId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "MainCodgHistory");

            migrationBuilder.DropTable(
                name: "MainCodgRule");

            migrationBuilder.RenameColumn(
                name: "CodgDictName",
                table: "MainCodgDict",
                newName: "Name");

            migrationBuilder.RenameColumn(
                name: "CatgeCodg",
                table: "MainCodgDict",
                newName: "Code");

            migrationBuilder.AddColumn<bool>(
                name: "DefaultFlag",
                table: "MainCodgDict",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.CreateTable(
                name: "MainCodgDictRule",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    CodgDictId = table.Column<Guid>(type: "uuid", nullable: true),
                    CodgDictRuleType = table.Column<int>(type: "integer", nullable: false),
                    DefaultValue = table.Column<string>(type: "text", nullable: true),
                    Format = table.Column<string>(type: "text", nullable: true),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainCodgDictRule", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainCodgDictRule_MainCodgDict_CodgDictId",
                        column: x => x.CodgDictId,
                        principalTable: "MainCodgDict",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_MainCodgDictRule_CodgDictId",
                table: "MainCodgDictRule",
                column: "CodgDictId");
        }
    }
}
